package com.ktjy.auth.handler;

import com.alibaba.fastjson.JSON;
import com.ktjy.base.R;
import com.ktjy.util.WebUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

/**
 * 认证失败处理器
 *
 * @author LILIBO
 * @since 2023-02-27
 */
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {

    /**
     * 认证失败
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) {
        WebUtil.writerString(response, JSON.toJSONString(R.failure(HttpStatus.UNAUTHORIZED.value(), "认证失败请重新登录")));
    }

}
